package com.hghivln.blog.common.context;

import lombok.Data;

import com.hghivln.blog.entity.Log;

/**
 * 使用ThreadLocal记录线程上下文
 */
@Data
public class ThreadContext {

    /**
     * 日志实体
     */
    private Log log;

    /**
     * 线程本地内存中的变量
     */
    private static final ThreadLocal<ThreadContext> threadLocal = new ThreadLocal<>();

    public static ThreadContext get() {
        if (threadLocal.get() == null) {
            ThreadContext context = new ThreadContext();
            threadLocal.set(context);
        }
        return threadLocal.get();
    }

    public void remove() {
        threadLocal.remove();
    }

}
